﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace PhysicsSimulator
{
    public partial class SaveMomentPage : PhoneApplicationPage
    {
        List<MomentData> datas;
        List<ItemListDataLoadMoment> load_datas;

        public SaveMomentPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            datas = MomentData.Load();
            load_datas = new List<ItemListDataLoadMoment>();
            foreach (MomentData data in datas)
            {
                ItemListDataLoadMoment load_data = new ItemListDataLoadMoment(data);
                load_datas.Add(load_data);
            }

            _data_list.ItemsSource = load_datas;
            base.OnNavigatedTo(e);
        }

        private void _data_list_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (_data_list.SelectedIndex != -1)
            {
                Settings.TurnableVelocity = datas[_data_list.SelectedIndex].TurnableVelocity;
                Settings.TurnableWeight = datas[_data_list.SelectedIndex].TurnableWeight;
                Settings.TurnableRadial = datas[_data_list.SelectedIndex].TurnableRadial;
                Settings.moment_save_active = false;
                NavigationService.GoBack();
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (_data_list.ItemContainerGenerator == null) return;
            var selectedListBoxItem = _data_list.ItemContainerGenerator.ContainerFromItem(((MenuItem)sender).DataContext) as ListBoxItem;
            if (selectedListBoxItem == null) return;
            int selectedIndex = _data_list.ItemContainerGenerator.IndexFromContainer(selectedListBoxItem);


            load_datas.RemoveAt(selectedIndex);

            _data_list.ItemsSource = new List<ItemListDataLoad>();
            _data_list.ItemsSource = load_datas;

            //if (selectedIndex > 0)
            //{
            //    _data_list.ScrollIntoView(load_datas[selectedIndex -1]);
            //}

            datas.RemoveAt(selectedIndex);
            MomentData.SaveData(datas);
        }
    }
}